﻿using QLBH.Models;
using QLBH.Modules;
using QLBH.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLBH.Controllers
{
    public class GoodController
    {
        private ucGood view;
        public GoodController(ucGood view)
        {
            this.view = view;
        }
        public void LoadData()
        {
            try
            {
                List<GoodModel> result = WSAccess.GetData<List<GoodModel>>(MainForm.Instance.Shop_id.ToString()+"/goods");
                view.DisplayData(result);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void Add() {
            try
            {
                frmAdd_Edit_Good adddlg = new frmAdd_Edit_Good(eType.ADD);
                if (adddlg.ShowDialog() == DialogResult.OK)
                {
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Edit()
        {
            try
            {
                frmAdd_Edit_Good editdlg = new frmAdd_Edit_Good(eType.EDIT);
                if (view.getCurrentSelectedItem() != null)
                    editdlg.Model = view.getCurrentSelectedItem();
                else
                    this.LoadData();
                if (editdlg.ShowDialog() == DialogResult.OK)
                {
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Delete()
        {
            try
            {
                if (MessageBox.Show("Bạn Có Muốn Xóa Không?", "Delete ConFirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    WSAccess.Delete<GoodModel>(view.getCurrentSelectedItem().Id + "/good/delete", view.getCurrentSelectedItem());
                    this.LoadData();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
